package Other.DeleteNode;

//19. 删除链表的倒数第N个节点
public class Delete {
    
}

class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
}


//一次遍历删除链表的倒数某个值
//通过快慢指针
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode headEmpty = new ListNode(-1); 
        headEmpty.next = head;
        ListNode fast = headEmpty;
        ListNode slow = headEmpty;
        for (int i = 0; i < n; i++) {
            fast=fast.next;
        }
        while(fast!=null){
            if(fast.next==null){
                slow.next = slow.next.next;
            }
            slow=slow.next;
            fast=fast.next;
        }
        return headEmpty.next;
    }
}